﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Newtonsoft.Json;
using System.Web;
using HMIS.FrameWork.DomainBase;
using System.Web.Script.Serialization;
using System.IO;
using System.Globalization;
using HMIS.FrameWork;

namespace HMIS.FrameWork
{
	public static class FormatJsonExtension
	{
		public static FormatJsonResult JsonFormat(this Controller c, object data, string[] exceptMemberName)
		{
			FormatJsonResult result = new FormatJsonResult();
			result.Data = data;
			result.ExceptMemberName = exceptMemberName;
			return result;
		}

		public static FormatJsonResult JsonFormat(this Controller c, object data)
		{
			return JsonFormat(c, data, null);
		}
		public static List<DataFilter> DesDataFilter(this Controller c, string json)
		{
			if (string.IsNullOrEmpty(json))
				return null;
			return JsonConvert.DeserializeObject<List<DataFilter>>(json);
		}
	}

	public class FormatJsonResult : ActionResult
	{
		public string[] ExceptMemberName { get; set; }
		public Object Data { get; set; }
		public override void ExecuteResult(ControllerContext context)
		{
			if (context == null)
			{
				throw new ArgumentNullException("context");
			}
			HttpResponseBase response = context.HttpContext.Response;
			response.ContentType = "application/json";

			StringWriter sw = new StringWriter();
			JsonSerializer serializer = JsonSerializer.Create(
				new JsonSerializerSettings
				{
					Converters = new JsonConverter[] { new Newtonsoft.Json.Converters.JavaScriptDateTimeConverter() },
					ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
					NullValueHandling = NullValueHandling.Ignore,
					ContractResolver = new NHibernateContractResolver(ExceptMemberName)
				}
				);

			using (JsonWriter jsonWriter = new JsonTextWriter(sw))
			{
				jsonWriter.Formatting = Formatting.Indented;
				serializer.Serialize(jsonWriter, Data);
			}
			response.Write(sw.ToString());
		}
	}
}
